package cm.mefobe.android.telecomcost.adapter;

import java.util.ArrayList;
import cm.mefobe.android.telecomcost.entities.TimePeriod;
import cm.mefobe.android.telecomcost.view.TimePeriodRadioListView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TimePeriodRadioAdapter extends ArrayAdapter<TimePeriod> {
	private ArrayList<TimePeriod> items;
	private Context _context;
	private TimePeriod selectedPeriod;
	
	public TimePeriodRadioAdapter(Context context, int textViewResourceId,
			ArrayList<TimePeriod> items,
			TimePeriodRadioListView timePeriodRadioListView) {
		super(context, textViewResourceId, items);
		this.items = items;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public TimePeriodRadioAdapter(Context context, int resource,
			int textViewResourceId, ArrayList<TimePeriod> objects) {
		super(context, resource, textViewResourceId, objects);
		this.items = objects;
		_context = context;
	}

	public TimePeriod getSelectedPeriod() {
		return selectedPeriod;
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public TimePeriodRadioAdapter(Context context, int textViewResourceId,
			ArrayList<TimePeriod> objects) {
		super(context, textViewResourceId, objects);
		_context = context;
		this.items = objects;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			Log.i("TimePeriodAdapter", "creating new TimePeriod_row_view");
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(
					cm.mefobe.android.telecomcost.R.layout.standard_view,
					null);
		} else {
			Log.i("TimePeriodAdapter", "using existing TimePeriod_row_view");
		}

		final TimePeriod period = items.get(position);
		if (period != null) {
			final ViewGroup _parent = parent;
			final TextView label = (TextView) v
					.findViewById(cm.mefobe.android.telecomcost.R.id.label);
			if (label != null) {				
				label.setText(period.toString());
				label.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						Log.i("TimePeriodAdapter", "label clicked");
						setPeriod(period,position,_parent);
					}
				});
			}
			
			v.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Log.i("TimePeriodAdapter", "view clicked");
					setPeriod(period,position,_parent);
				}
			});
		}
		return v;
	}

	private void setPeriod(TimePeriod period, int position,ViewGroup parent) {
		if (parent != null) {
			selectedPeriod = period;
			Log.i("TimePeriodAdapter", "period " + period + " selected...");
			((TimePeriodRadioListView) parent).setSelection(position);
		} else {
			Log.i("TimePeriodAdapter", "callback undefined");
		}
	}
}
